function MClientUtils() {
};

MClientUtils.searchClient = function(gui) {
  return { search_client_name: '',
           search_given_name: '',
           search_nationality: '',
           search_phone: '',
           search_school: '',
           search_staff: '',
           search_agent: '',
          
           search_visa_client_num: '',
           search_visa_app_num: '',
           search_visa_type: '',
           search_submit_from: 0,
           search_submit_to: 0
         };
};

MClientUtils.searchClient2 = function(gui) {
  return { search_client_name: '',
           search_given_name: '',
           search_pass_num: '',
           search_date_birth: 0
         };
};

MClientUtils.searchVisa = function(gui) {
  return { search_client_name: '',
           search_given_name: '',
           search_nationality: '',
           search_phone: '',
           search_school: '',
           search_staff: '',
           search_agent: '',
          
           search_visa_client_num: '',
           search_visa_app_num: '',
           search_visa_type: '',
           search_submit_from: 0,
           search_submit_to: 0,
           
           search_pending: 1,
           search_issued: 1,
           search_declined: 1,
         };
};

MClientUtils.countIt = function(r) {
  var due_soon = document.getElementById('basemenu_due_soon');
  var expiring_visa = document.getElementById('basemenu_expiring_visa');
  var next_school_payment = document.getElementById('basemenu_next_school_payment');
  DOMUtils.removeChildElements(due_soon);
  DOMUtils.removeChildElements(expiring_visa);
  DOMUtils.removeChildElements(next_school_payment);
  r._custom('countIt',
            '',
            function(result, params) { if (due_soon && (result.expiring_visa > 0 || result.next_school_payment > 0)) {
                                         due_soon.appendChild(document.createTextNode(' (' + (parseInt(result.expiring_visa, 10) + parseInt(result.next_school_payment, 10)) + ')'));
                                       }
                                       if (expiring_visa && result.expiring_visa > 0) {
                                         expiring_visa.appendChild(document.createTextNode(' (' + result.expiring_visa + ')'));
                                       }
                                       if (expiring_visa && result.next_school_payment > 0) {
                                         next_school_payment.appendChild(document.createTextNode(' (' + result.next_school_payment + ')'));
                                       }
                                     }, null);
};

MClientUtils.join = function(parts) {
  var result = '';
  for (var i = 0, il = parts.length; i < il; i++) {
    var tmp = parts[i];
    if (tmp == '') {
      continue;
    }
    
    if (result != '') {
      result += ', ';
    }
    result += tmp;
  }
  return result;
};

MClientUtils.verifyClient = function(obj, gui) {
  var result = true;
  var tmp = /[^a-zA-Z]/g;
  this.clearError(gui.name_family);
  if (obj.name_family == '') {
    this.showError('No Empty', gui.name_family);
    result = false;
  } else if (tmp.test(obj.name_family) == true) {
    this.showError('Letters Only', gui.name_family);
    result = false;
  }
  
  tmp = /[^a-zA-Z]/g;
  this.clearError(gui.name_first);
  if (obj.name_first == '') {
    this.showError('No Empty', gui.name_first);
    result = false;
  } else if (tmp.test(obj.name_first) == true) {
    this.showError('Letters Only', gui.name_first);
    result = false;
  }
  
  tmp = /[^0-9]/g;
  this.clearError(gui.phone1);
  if (tmp.test(obj.phone1) == true && obj.phone1 != '') {
    this.showError('Digits Only', gui.phone1);
    result = false;
  }
  
  tmp = /[^0-9]/g;
  this.clearError(gui.phone2);
  if (tmp.test(obj.phone2) == true && obj.phone2 != '') {
    this.showError('Digits Only', gui.phone2);
    result = false;
  }
  
  tmp = /[^0-9]/g;
  this.clearError(gui.phone3);
  if (tmp.test(obj.phone3) == true && obj.phone3 != '') {
    this.showError('Digits Only', gui.phone3);
    result = false;
  }
  
  tmp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
  this.clearError(gui.email);
  if (tmp.test(obj.email) == false && obj.email != '') {
    this.showError('Not Valid Email', gui.email);
    result = false;
  }
  return result;
};

MClientUtils.verifyUser = function(obj, gui) {
  var result = true;
  var tmp = /[^a-zA-Z]/g;
  this.clearError(gui.account);
  if (obj.account == '') {
    this.showError('No Empty', gui.account);
    result = false;
  } else if (tmp.test(obj.account) == true) {
    this.showError('Letters Only', gui.account);
    result = false;
  }
  
  tmp = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
  this.clearError(gui.email);
  if (tmp.test(obj.email) == false && obj.email != '') {
    this.showError('Not Valid Email', gui.email);
    result = false;
  }
  return result;
};


MClientUtils.clearError = function(input) {
  input.style.border = '';
  while (input.parentNode && input.parentNode.childNodes.length > 3) {
    input.parentNode.removeChild(input.parentNode.lastChild);
  }
};

MClientUtils.showError = function(str, input) {
  var div = document.createElement('div');
  div.style.fontWeight = 'bold';
  div.style.color = '#ff0000';
  div.style.paddingTop = '2px';
  div.style.paddingBottom = '5px';
  div.appendChild(document.createTextNode(str));
  
  input.style.borderColor = '#ff0000';
  input.parentNode.appendChild(div);
};
